﻿/*  Copyright (C) 2012  Mark Hamburger
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */

using System;
using System.Net;
using System.IO;
using System.Data;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace removeav2012
{
    public partial class downloadForm : Form
    {
        public downloadForm()
        {
            InitializeComponent();
        }

        private void downloadButton_Click(object sender, EventArgs e)
        {  
            string aPath = Application.StartupPath;     //find the path the app is being run from
            WebClient client = new WebClient();  

            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);  
            client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);  

            // Starts the download  

            client.DownloadFileAsync(new Uri("http://download.bleepingcomputer.com/reg/FixNCR.reg"), aPath + "\\FixNCR.reg");  

            downloadButton.Text = "Download In Process";  
            downloadButton.Enabled = false;   
        }

        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            double bytesIn = double.Parse(e.BytesReceived.ToString());
            double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
            double percentage = bytesIn / totalBytes * 100;

            progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
        }

        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            string aPath = Application.StartupPath + "\\FixNCR.reg";
            MessageBox.Show("Download Completed");

            if (File.Exists(aPath))
            {
                fixExeButton.Enabled = true;
            }
                
            downloadButton.Text = "Start Download";
            downloadButton.Enabled = true;
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void fixExeButton_Click(object sender, EventArgs e)
        {
            string aPath = Application.StartupPath; //path the app is running from
            string ncrReg = aPath + "\\FixNCR.reg"; //where fixncr.reg should be
            string ncrRegexec = " \"" + ncrReg + "\"";       //the path regedit should look for fixncr

            Process regeditProcess = Process.Start("regedit.exe", ncrRegexec); //insert registry fix for exe's
            regeditProcess.WaitForExit();

            Close();
        }

        private void downloadForm_Load(object sender, EventArgs e)
        {

        }
        
    }
}
